﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class ThemUserForm : Form
    {
        private int mode=-1;
        private _DB.NguoiDung nguoiDung = null;
        public ThemUserForm()
        {
            InitializeComponent();
        }

        public ThemUserForm(_DB.NguoiDung u, int _mode){
            InitializeComponent();
            nguoiDung = u;
            mode = _mode;
            txtMa.Text = u.MaNguoiDung;
            //txtUsername.Text = u.Username;
            txtPassword.Text = u.Password;
            txtPassword2.Text = u.Password;
            if(u.Ten!=null)
                txtName.Text = u.Ten;
            if(u.NgaySinh!=null)
                dtpickBirthdate.Value = (DateTime)u.NgaySinh;
            if(u.Email!=null)
                txtEmail.Text = u.Email;
            if(u.ThongTinNhanVien!=null)
                txtInfo.Text = u.ThongTinNhanVien;
            if(u.ViTriPhongBan!=null)
                txtPosition.Text = u.ViTriPhongBan;
            if(u.TrangThai!=null)
                txtStatus.Text = u.TrangThai;
            if(mode == 0){
                txtMa.ReadOnly = true;
                //txtUsername.ReadOnly = true;
                txtPassword.ReadOnly = true;
                txtPassword2.ReadOnly = true;
                txtName.ReadOnly = true;
                dtpickBirthdate.Enabled = false;
                txtEmail.ReadOnly = true;
                txtInfo.ReadOnly = true;
                txtPosition.ReadOnly = true;
                txtStatus.ReadOnly = true;
                cbGroup.Enabled = false;
                
            }
            if (mode == 1)
                txtMa.ReadOnly = true;
           
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            
            if (mode == 0)
                this.Close();
            else if (cbGroup.Items.Count == 0)
            {
                MessageBox.Show("Không có nhóm người dùng nào.");
                this.Close();
            }
            else if(cbPhongBan.Items.Count==0)
            {
                MessageBox.Show("Không có phòng ban dùng nào.");
                this.Close();
            }
            else if (cbGroup.SelectedIndex >= 0 &&cbPhongBan.SelectedIndex>=0)
            {
                if (txtMa.Text.Trim().Length==0 || txtPassword.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Cần điền đầy đủ thông tin");
                    return;
                }
                if (txtPassword.Text != txtPassword2.Text)
                {
                    MessageBox.Show("Mật mã không khớp.");
                    return;
                }

                _DB.NguoiDung newUser = new _DB.NguoiDung();
                newUser.MaNguoiDung = txtMa.Text.Trim();
                //newUser.Username = txtUsername.Text.Trim();
                newUser.Password = txtPassword.Text.Trim();

                newUser.NhomNguoiDung_ID = ((_DB.NhomNguoiDung)cbGroup.SelectedItem).NhomNguoiDung_ID;
                newUser.MaPhongBan = ((_DB.PhongBan)cbPhongBan.SelectedItem).MaPhongBan;
                newUser.ViTriPhongBan = txtPosition.Text;
                newUser.Ten = txtName.Text;
                newUser.NgaySinh = dtpickBirthdate.Value;
                newUser.Email = txtEmail.Text;
                newUser.ThongTinNhanVien = txtInfo.Text;
                newUser.TrangThai = txtStatus.Text;
                //userInsert(newUser);
                if (mode ==-1)
                {
                 //   MessageBox.Show("insert");
                    _DB.NguoiDungDB.insert(newUser);
                }
                else if(mode==1)
                {
                   // newUser.MaNguoiDung = _maNguoiDung;
                    _DB.NguoiDungDB.update(newUser);
                }
                this.Close();
            }

        }

        private void ThemUserForm_Load(object sender, EventArgs e)
        {
            _DB.DataClasses1DataContext db = new _DB.DataClasses1DataContext();
            cbGroup.Items.Clear();
            foreach (_DB.NhomNguoiDung ug in db.NhomNguoiDungs)
            {
                if (ug.deleted == true)
                    continue;
                cbGroup.Items.Add(ug);
            }
            cbPhongBan.Items.Clear();
            foreach (_DB.PhongBan ug in db.PhongBans)
            {
                if (ug.deleted == true)
                    continue;
                cbPhongBan.Items.Add(ug);
            }
            if (nguoiDung != null)
            {
                for (int i = 0; i < cbGroup.Items.Count; i++)
                {

                    if (((_DB.NhomNguoiDung)cbGroup.Items[i]).NhomNguoiDung_ID == nguoiDung.NhomNguoiDung_ID)
                    {


                        cbGroup.SelectedIndex = i;
                        break;
                    }

                }
                for (int i = 0; i < cbPhongBan.Items.Count; i++)
                {

                    if (((_DB.PhongBan)cbPhongBan.Items[i]).MaPhongBan == nguoiDung.MaPhongBan)
                    {


                        cbPhongBan.SelectedIndex = i;
                        break;
                    }

                }
            }
            else
                if (cbGroup.Items.Count > 0 && cbPhongBan.Items.Count>0)
                {
                    cbGroup.SelectedIndex = 0;
                    cbPhongBan.SelectedIndex = 0;
                }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
